import {
    apiLogin,
    apiLogout
} from '@/api/user';
import {
    loginUserInfo
  } from '@/api/system/account-manage.js'
import {
    setToken,
    getToken
} from '@/libs/util';

export default {
    state: {
        userName: '',
        userId: '',
        avatarImgPath: '',
        token: getToken(),
        access: '',
        hasGetInfo: false,
        unreadCount: 0,
        messageUnreadList: [],
        messageReadedList: [],
        messageTrashList: [],
        messageContentStore: {},
        permissions:[],
        
    },
    mutations: {
        setAvatar(state, avatarPath) {
            state.avatarImgPath = avatarPath;
        },
        setUserId(state, id) {
            state.userId = id;
        },
        setPermissions(state,data){
            state.permissions = data;
        },
        setUserName(state, name) {
            state.userName = name;
        },
        setAccess(state, access) {
            state.access = access;
        },
        setToken(state, token) {
            state.token = token;
            setToken(token);
        },
        setHasGetInfo(state, status) {
            state.hasGetInfo = status;
        },
        setMessageCount(state, count) {
            state.unreadCount = count;
        },
        setMessageUnreadList(state, list) {
            state.messageUnreadList = list;
        },
        setMessageReadedList(state, list) {
            state.messageReadedList = list;
        },
        setMessageTrashList(state, list) {
            state.messageTrashList = list;
        },
        updateMessageContentStore(state, {
            msg_id,
            content
        }) {
            state.messageContentStore[msg_id] = content;
        },
        moveMsg(state, {
            from,
            to,
            msg_id
        }) {
            const index = state[from].findIndex(_ => _.msg_id === msg_id);
            const msgItem = state[from].splice(index, 1)[0];
            msgItem.loading = false;
            state[to].unshift(msgItem);
        }
    },
    getters: {
        messageUnreadCount: state => state.messageUnreadList.length,
        messageReadedCount: state => state.messageReadedList.length,
        messageTrashCount: state => state.messageTrashList.length
    },
    actions: {
        // 登录
        handleLogin({
                        commit
                    }, {
                        username,
                        password,
                        captcha,
                        uuid
                    }) {
            return new Promise((resolve, reject) => {
                apiLogin({
                    username,
                    password,
                    captcha,
                    uuid
                }).then(res => {
                    resolve(res);
                }).catch(err => {
                    reject(err);
                });
            });
        },
        // 退出登录
        handleLogOut({
                         state,
                         commit
                     }) {
            return new Promise((resolve, reject) => {
                apiLogout().then(() => {
                    localStorage.removeItem('token')
                    resolve();
                }).catch(err => {
                    reject(err);
                });
            });
        },
        //获取当前账号用户信息
        getUserInfo({ commit }){
            return new Promise((resolve, reject) => {
                loginUserInfo().then(res => {
                    commit('setUserName',res.data.user.nickname)
                    resolve(res);
                }).catch(err => {
                    reject(err);
                });
            });
        }
    }
};
